﻿using UnityEngine;
using System.Collections;

public class HoverUnitInformUI : MonoBehaviour 
{
    [HideInInspector]
    public GameObject m_pOwner = null;

    int m_iBeforeCSCount;
    void Start()
    {
        for (int i = 0; i < transform.childCount; ++i)
            if (transform.GetChild(0).GetChild(i).GetComponent<UILabel>())
                transform.GetChild(0).GetChild(i).GetComponent<UILabel>().depth = transform.GetChild(0).GetComponent<UISprite>().depth + 1;

        TextingCurStatus();

        transform.localScale = Vector3.one;

        GetComponent<UIFollowTarget>().target = m_pOwner.transform;
        GetComponent<UIFollowTarget>().gameCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
        GetComponent<UIFollowTarget>().uiCamera = GameObject.Find("Camera").GetComponent<Camera>();

        transform.GetChild(0).Find("Face").GetComponent<UISprite>().spriteName = m_pOwner.GetComponent<UnitStatus>().m_unitData.strFaceName;

        transform.GetChild(0).FindChild("Name").GetComponent<UILabel>().text = string.Format("{0} {1}", m_pOwner.GetComponent<UnitStatus>().m_unitData.strFirstName, m_pOwner.GetComponent<UnitStatus>().m_unitData.strLastName);
    }

    // Update is called once per frame
    void Update()
    {
        transform.GetChild(0).Find("Rank").GetComponent<UISprite>().spriteName = string.Format("Rank{0}", m_pOwner.GetComponent<UnitStatus>().m_unitData.ClassStatus.AssnStatus.iRank);
        transform.GetChild(0).Find("Weapon").GetComponent<UISprite>().spriteName = m_pOwner.GetComponent<UnitStatus>().m_WeaponData.m_strWeaponName;
        transform.GetChild(0).Find("Mag").GetComponent<UILabel>().text = string.Format("x {0}", m_pOwner.GetComponent<UnitStatus>().m_unitData.iHavMagazine);

        transform.localScale = Vector3.one;
        TextingCurStatus();

        if (m_pOwner.GetComponent<UnitStatus>().m_unitData.m_List_ConditionStrange.Count != m_iBeforeCSCount)
        {
            while(true)
            {
                if (transform.FindChild("Card_ConditionStrange(Clone)") != null)
                {
                    Destroy(transform.FindChild("Card_ConditionStrange(Clone)").gameObject);
                }else
                    break;
            }

            for(int i =0; i < m_pOwner.GetComponent<UnitStatus>().m_unitData.m_List_ConditionStrange.Count; ++i)
            {
                (Instantiate(Resources.Load("Prefaps/GameUI/Card_ConditionStrange") as GameObject) as GameObject).transform.parent = transform;
                transform.GetChild(transform.childCount - 1).GetComponent<HoverCS>().m_iPos = i;

                string backStr;
                switch (m_pOwner.GetComponent<UnitStatus>().m_unitData.m_List_ConditionStrange[i].GetComponent<ConditionStrange>().iCS_ID)
                {
                    case CONDITION_STRANGE.AMMO_LOW:
                        transform.GetChild(transform.childCount - 1).GetComponent<HoverCS>().m_strSpriteName = "Ammo_Low";
                        break;

                    case CONDITION_STRANGE.AMMO_OUT:
                        transform.GetChild(transform.childCount - 1).GetComponent<HoverCS>().m_strSpriteName = "Ammo_Low";
                        break;
                }
            }
            

        }

        m_iBeforeCSCount = m_pOwner.GetComponent<UnitStatus>().m_unitData.m_List_ConditionStrange.Count;

    }

    void TextingCurStatus()
    {
        UnitStatus unitStatus = m_pOwner.GetComponent<UnitStatus>();
        
        string strBackText;
        Color BackColor;

        //Current HP
        if (unitStatus.m_unitData.iCurrentHP == unitStatus.m_unitData.iMaxHP)
        {
            strBackText = "HEALTHY";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentHP >= (unitStatus.m_unitData.iMaxHP * 3f / 4f))
        {
            strBackText = "SCRATCHY";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentHP >= (unitStatus.m_unitData.iMaxHP * 1f / 2f))
        {
            strBackText = "WOUNDED";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentHP >= (unitStatus.m_unitData.iMaxHP * 1f / 4f))
        {
            strBackText = "CRITICAL";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else
        {
            strBackText = "DYING";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }

        transform.GetChild(0).FindChild("CurHealth").GetComponent<UILabel>().text = strBackText;
        //transform.GetChild(0).FindChild("CurHealth").GetComponent<UILabel>().color = BackColor;


        //Current MORALE
        if (unitStatus.m_unitData.iCurrentMorale == unitStatus.m_unitData.iMaxMorale)
        {
            strBackText = "INSPIRED";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentMorale >= (unitStatus.m_unitData.iMaxMorale * 3f / 4f))
        {
            strBackText = "ENCOURAGED";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentMorale >= (unitStatus.m_unitData.iMaxMorale * 1f / 2f))
        {
            strBackText = "NORMAL";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentMorale >= (unitStatus.m_unitData.iMaxMorale * 1f / 4f))
        {
            strBackText = "DISPIRITED";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }
        else
        {
            strBackText = "PANIC";
            //BackColor = new Color(75 / 255f, 150 / 255f, 75 / 255f);
        }

        transform.GetChild(0).FindChild("CurMorale").GetComponent<UILabel>().text = strBackText;
        //transform.GetChild(0).FindChild("CurMorale").GetComponent<UILabel>().color = BackColor;

    }
}
